﻿<?php


class RoleApp extends BaseApp
{
    function index()
    {

 		$TB_role = &$this->_model('role');  
		  
		$roledata = $TB_role->show();
		
        $this->assign('roledata', $roledata);		 
		 		     	
        $this->assign('role_file', $this->role_con(ROLE_CON_FILE));
					    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('role_list.view.php');
    }

    function edit()
    {
		
		$id = $_GET['id'];
		$TB_role = &$this->_model('role');	 
		
		$roledata = $TB_role->oneshow($id);	
		$roledata['config'] = @unserialize($roledata['config']);
		
	    $this->assign('roledata', $roledata);	
			    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('role_edit.view.php');
    }

    function add()
    {
		
	
	    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('role_add.view.php');
    }

    function power()
    {
		
	    $id = $_GET['id'];
		$pname = $_GET['v'];

		$TB_role = &$this->_model('role');
		$roledata = $TB_role->oneshow($id);
		$roledata['powerdb'] = @unserialize($roledata['powerdb']);

		unset($GLOBALS['roledata_powerdb']);
		$GLOBALS['roledata_powerdb'] = $roledata['powerdb'];

        $this->assign('rolename', $roledata['role_name']); 
		
        $this->assign('pname', $pname); 	
        $this->assign('powerid', $id);     
        $this->assign('powerdata', $this->get_powerdata($pname));
			    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('power_list.view.php');
    }

    function powerpost()
    {
		if(!isset($_POST['id']))
		{
		    $_POST['id']='';
		}
		if(!isset($_POST['power']))
		{
		    $_POST['power']='';
		}		
		
		if(!isset($_POST['v']))
		{
		    $_POST['v']='';
		}			
	    $id = $_POST['id'];
        $power = $_POST['power'];
		$pname = $_POST['v'];
		
		$powerdata = array();


		if(empty($power))
		{
		  $power = array();
		}

		foreach($power as $key => $value)
		{
		  $str_array = explode('|',$value);
		  $key1 = $str_array[0];
		  $value1 =  explode(':',$str_array[1]);
		  if(!isset($str_array[2]))
		  {
		  $str_array[2] = '0';
		  }
		  
		      foreach($value1 as $key2 => $value2)
			  {			      
			      if($value2)
				  {
				      if($str_array[2])
					  {
				      $powerdata[$key1.':0'][$value2] = array('set' => 1,'url'=>''); 					  
					  }
				      $powerdata[$key1.':'.$str_array[2]][$value2] = array('set' => 1,'url'=>''); 
				  }
			      
			  }		  
		}

/*       $powerdata_temp = array();

        $powerdata_temp = $this->get_powerdata($pname);
  
		foreach($powerdata_temp as $key => $value)
		{

              
		      foreach($value['act'] as $key2 => $value2)
			  {
			  
			  		$temp_array =  explode(':',$key2);
		            foreach($temp_array as $key3 => $value3)
			        {
			              if($value3&&(!isset($powerdata[$value['app']][$value3])||!isset($powerdata[$value['app']][$value3]['set'])))
				          {
				           $powerdata[$value['app']][$value3] = array('set' => 0,'url'=>''); 
				          }
			      
			        }		 

			      
			  }		  
		}
*/


		 $TB_role = &$this->_model('role');
		 $roledata = $TB_role->oneshow($id);
		 $roledata['powerdb'] = @unserialize($roledata['powerdb']);
	 
		 $roledata['powerdb'][$pname] = $powerdata;
	 
		 $roledata['powerdb'] = serialize($roledata['powerdb']);

         $TB_role->editpower($id,$roledata['powerdb']);
		 $this->write_cache();		 
		 $this->index();		 


    }



    function addpost()
    {

	     $data = $_POST['data'];
         $tempdata['name'] = $data['name'];			 		 
		 unset($data['name']);	 	 
		 $TB_role = &$this->_model('role');
		 $icon = $TB_role->check($tempdata['name']);

		 if(!$icon['role_id'])
		 {		 
		 $data['config'] = serialize($data);
         $data['name'] = $tempdata['name'];			   		 
		 $TB_role->post($data);		 
		// $this->write_cache();
		 $this->index();		 
		 }
		 else
		 {
		 $notice = &get_message(array('msg'=>'False!','url'=>'','title'=>'Notice'));
		 $notice ->display();
		 } 	 
		


    }

    function editpost()
    {

	     $data = $_POST['data'];
         $tempdata['name'] = $data['name'];			 		 
		 $id = intval($data['id']);	 
		 unset($data['name'],$data['id']);		 
		 $TB_role = &$this->_model('role');
		 $icon = $TB_role->check($tempdata['name']);
		 if($icon['role_id']==$id)
		 {		 
		 $data['config'] = serialize($data);
         $data['name'] = $tempdata['name'];				
		 $TB_role->editaction($id,$data);
		 
		// $this->write_cache();
		 $this->index();	 
		 
		 }
		 else
		 {
		 $notice = &get_message(array('msg'=>'False!','url'=>'','title'=>'Notice'));
		 $notice ->display();
		 } 	 		 


		 

    }




    function lname()
    {
		 $data = array();
		 
	     $id = $_GET['id'];
		 $data['name'] = $_GET['v'];
		 
		 $TB_role = &$this->_model('role'); 	 		
		 $TB_role->editname($id,$data); 
		// $this->write_cache();		 
		 $this->index();			  

    }
	


    function del()
    {

	     $id = $_GET['id'];
		 $TB_role = &$this->_model('role');
			 
		 $TB_role->del($id);	
		 	 
		// $this->write_cache();		 
		 $this->index();	  

    }

    function get_powerdata($name)
    {
         
        $default_filename = ROOT_PATH . '/lib/power/'.$name.'.power.php';  
		$filename = ROOT_PATH . '/data/power/data/'.$name.'.power.php'; 			 
      //  if((file_exists($default_filename) && filemtime($default_filename) > filemtime($filename)) )
		//{
		  $this->write_power_cache();
		
		//}

		if(is_file($filename))
	    {
	     $powerdata = include($filename); 
		}
		else
	    {
          exit('Missing PowerFile');
	    } 		
        return $powerdata;

    }	

    function write_power_cache()
    { 
		$TB_module = &$this->_model('module'); 		
		$infos = $TB_module->show();
		$infos_temp = array();		
		array_var_sort($infos,'sort');		
		foreach($infos as $key => $value)
		{
		   $infos_temp[$value['appname']] = $value;
		}	
		
		$default_adminpower = include(ROOT_PATH .'/lib/power/admin.power.php');
		$default_scuppower = include(ROOT_PATH .'/lib/power/scup.power.php');


		foreach($infos_temp as $key=>$value)
		{
           $catdata = $GLOBALS['SCup']->_get_category_data($value["appname"]);
		   if(!isset($catdata[0]['cat_id']))
		   {
               $catdata =   array (
                                     0 => 
                                        array (
                                              'cat_id' => '0',
                                              'cat_name' => '',

                                              ),
                                  );		   
		   }		
		   foreach($catdata as $key1=>$value1)
		   {
		   
	   $default_adminpower[] = array 
       ('name' => $value["name"].'----'.$value1['cat_name'],
	    'app' => $value["appname"],
	    'catid' => $value1['cat_id'],		
		'act' => array 
                     ('index'=>'查看列表',
					  'add:addpost:kindeditor:kindeditorupfile:upfile:pupfile'=>'添加',	
					  'del'=>'删除',
					  'edit:editpost:kindeditor:kindeditorupfile:upfile:pupfile'=>'修改',
					  'cagrylist'=>'分类列表',
					  'cagryadd:cyaddpost'=>'分类添加',	
					  'delcatgy'=>'分类删除',	
					  'editcatgy:cyeditpost'=>'分类修改',	
					  'hui'=>'回复管理',	
					  'batch'=>'批量操作',						  					  																			
                     )
       );
	   $default_scuppower[] = array 
       ('name' => $value["alias"].'----'.$value1['cat_name'],
	    'app' => $value["appname"],
	    'catid' => $value1['cat_id'],			
		'act' => array 
                     ('index'=>'列表',
					  'show'=>'内容显示',	
					  'userlist'=>'用户中心内容列表',						  
					  'add:addpost:kindeditor:kindeditorupfile:upfile:pupfile'=>'添加',	
					  'del'=>'删除',
					  'edit:editpost:kindeditor:kindeditorupfile:upfile:pupfile'=>'修改',
					  'hui'=>'回复管理',						  					  											
                     )
       );		   
		   
		   }



		}


		$cache_filename_admin = ROOT_PATH .'/data/power/data/admin.power.php';
		$cache_filename_scup = ROOT_PATH .'/data/power/data/scup.power.php';	
		
	    write_file($cache_filename_admin, "<?php \nreturn " . var_export($default_adminpower , true) . ";\n?>");
	    write_file($cache_filename_scup, "<?php \nreturn " . var_export($default_scuppower , true) . ";\n?>");
		
		if(!is_writable($cache_filename_admin)||!is_writable($cache_filename_scup))
		{
		exit('file directory is not writed power');//文件目录权限不可写
	    }
    }	
		

    function cache()
    {
         
		 $this->write_cache();		 
		 $this->index();		 


    }	
    function write_cache()
    {
	
		$TB_role = &$this->_model('role');			
		$roledata = $TB_role->show();
		
		$roledata_temp = array();
		
	    $filebag = $this->role_con(ROLE_CON_FILE);

		foreach($roledata as $key => $value)
		{		 		 
		      $value['powerdb'] = @unserialize($value['powerdb']);
		 
		      
		 
		      foreach($filebag as $key1 => $value1)
		      {	
			   $roledata_temp[$value1][$value['role_id']]['role_name'] = $value['role_name'];
			   if(isset($value['config'][$value1]))
			   {
			   $roledata_temp[$value1][$value['role_id']]['config'] = $value['config'][$value1];			   
			   }
			   else
			   {
			   $roledata_temp[$value1][$value['role_id']]['config'] = '';			   
			   }
                if(isset($value['powerdb'][$value1]))
				{
		       $roledata_temp[$value1][$value['role_id']]['powerdb'] = $value['powerdb'][$value1];					
				}
				else
				{
				$roledata_temp[$value1][$value['role_id']]['powerdb'] = '';
				}
		  	     
		      }
		}

		foreach($filebag as $key1 => $value1)
		{	
		     foreach($roledata_temp[$value1] as $key2 => $value2)
			 {
		        $cache_filename = ROOT_PATH . '/data/power/'.$value1.'_'.$key2.'.power.php';
		        $tempdata = array();
				$tempdata[$key2] = $roledata_temp[$value1][$key2];
	            write_file($cache_filename, "<?php if(!defined('IN_PHPSCUP'))exit('hacking');  \nreturn " . var_export($tempdata , true) . ";\n?>");
		
		        if(!is_writable($cache_filename))
		        {
		         exit('file directory is not writed power');//文件目录权限不可写
	            }				 
			 }
		  	     
	    }		

    }


    function role_con($str)
    {
	      $array = array();
          $str_array = explode('|',$str);
	      foreach($str_array as $key => $value)
	      {
	            if($value)
		        {
			   
			   $array[$key] = $value;
		   
		        }
	 
	      }    
	   
	      return $array; 		

    }

  
}

function definerole($id)
{
        if(empty($id)) 
		{
		return false;
		}
		if(str_explode($id,ROLE_Y_ID)||str_explode($id,ROLE_P_ID) ||str_explode($id,ROLE_V_ID))
		{
		
		return true;
		}
		
        return false;

}


function str_explode($id,$str)
{

       $array = array(); 
       $str_array = explode('|',$str);
	   foreach($str_array as $key => $value)
	   {
	         if($value)
		     {			 
		          $array =  explode(':',$value);
			 	  if(in_array($id,$array))
				  {
				  return true;
				  }	
				  unset($array);	 
		     }
	 
	   } 

        return false;

}



function powercheck($app,$act,$scup,$catid=0)
{

		$str_array =  explode(':',$act);
		
		foreach($str_array as $key => $value)
		{         
		          if(!isset($GLOBALS['roledata_powerdb'][$scup][$app.':'.$catid][$value]['set']))
				  {
				  $GLOBALS['roledata_powerdb'][$scup][$app.':'.$catid][$value]['set']='';
				  }
			      if($value && $GLOBALS['roledata_powerdb'][$scup][$app.':'.$catid][$value]['set']==1)
				  {
                   	return true;			   
				  }
			      
		}		  

        return false;

}



?>